package com.qijian.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.aspose.cad.internal.u.x;
import com.qijian.common.core.domain.entity.SysDictData;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.StringUtils;
import com.qijian.system.domain.Organization;
import com.qijian.system.mapper.OrganizationMapper;
import com.qijian.system.service.IOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 组织Service业务层处理
 *
 * @author qijian
 * @date 2022-02-21
 */
@Service
@RequiredArgsConstructor
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

    private final OrganizationMapper organizationMapper;
    private final ISysDictDataService sysDictDataService;

    @Override
    public boolean importList(List<Organization> list) {
        Map<String, List<SysDictData>> stringListMap = listDictByType();
        List<Organization> organizations = organizationMapper.selectListInfo();
        Map<String, Long> stringLongMap = organizations.stream()
                .collect(Collectors.toMap(
                        Organization::getNumber,
                        Organization::getId
                ));
                list.stream().forEach(x -> {
                    if(StringUtils.isEmpty(x.getName())){
                        x.setName(x.getFName());
                    }
                    x.setStatus("0");
                    if (StringUtils.isNotEmpty(x.getFOrgForm())&&stringListMap.containsKey(x.getFOrgForm())) {
                        String dictValue = stringListMap.get(x.getFOrgForm()).get(0).getDictValue();
                        x.setFOrgForm(dictValue);
                    }
                    if(StringUtils.isNotEmpty(x.getNumber())&&stringLongMap.containsKey(x.getNumber())){
                        x.setId(stringLongMap.get(x.getNumber()));
                    }
                }
        );
        return this.saveOrUpdateBatch(list);
    }

    public Map<String, List<SysDictData>> listDictByType() {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("f_org_form");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(dictData);
        if (CollUtil.isEmpty(dictDataList)) {
            throw new ServiceException("E01:形态对应的字典未维护");
        }
        Map<String, List<SysDictData>> listMap = dictDataList.stream().collect(Collectors.groupingBy(e -> e.getDictLabel()));
        return listMap;
    }

    public Map<String, List<SysDictData>> listDictByTypeValue() {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("f_org_form");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(dictData);
        if (CollUtil.isEmpty(dictDataList)) {
            throw new ServiceException("E01:形态对应的字典未维护");
        }
        Map<String, List<SysDictData>> listMap = dictDataList.stream().collect(Collectors.groupingBy(e -> e.getDictValue()));
        return listMap;
    }



}
